package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.annotation.DisableLoginCheck;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.ReleaseSlip;
import com.zoshine.zzg.pojo.ReleaseSlipDetail;
import com.zoshine.zzg.pojo.pojostatus.ReleaseSlipStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.ReleaseSlipDetailService;
import com.zoshine.zzg.service.ReleaseSlipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 田沛凌
 * @time 2017/7/31 15:23
 */
@RestController
@RequestMapping( value = "releaseSlip",method = RequestMethod.POST)
public class ReleaseSlipController extends BaseController{

    private ReleaseSlipService releaseSlipService;
    private ReleaseSlipDetailService releaseSlipDetailService;
    @Autowired
    public void setReleaseSlipService(ReleaseSlipService releaseSlipService) {
        this.releaseSlipService = releaseSlipService;
    }
    @Autowired
    public void setReleaseSlipDetailService(ReleaseSlipDetailService releaseSlipDetailService) {
        this.releaseSlipDetailService = releaseSlipDetailService;
    }
    /**
     * 打印发放单
     * @param idStr 发放单ID集合
     */
    @RequestMapping("/printReleaseSlip")
    public BaseResponse printReleaseSlip(String idStr){

        if(idStr == null){
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id : idArr) {
            try {
                Long l = Long.parseLong(id);
                idList.add(l);
            } catch (NumberFormatException ex) {
                throw new AppException(ErrorMessage.REQUEST_PARAM_TYPE_ERROR);
            }
        }
        List<ReleaseSlip> releaseSlips=new ArrayList<>();
        for (Long id:idList){
            ReleaseSlip releaseSlip = releaseSlipService.get(id);
            releaseSlip.setCreateUser(super.getLoginUserId());
            //如果再次打印，原先数据不作处理
            if(releaseSlip.getStatus().equals(ReleaseSlipStatus.PENDING_RELEASE.getValue())){
                releaseSlips.add(releaseSlip);
            }
        }
        if(releaseSlips.size()>0){
            releaseSlipService.printReleaseSlip(releaseSlips);
        }
        //打印代码编写处

        return AppResponse.OK;
    }

    /**
     * 发放单列表显示
     * @param releaseSlip；
     * @return 发放单集合
     */
    @DisableLoginCheck
    @RequestMapping("/pageReleaseSlip")
    public BaseResponse pageReleaseSlip(ReleaseSlip releaseSlip){
        releaseSlip.setSupplierPower(super.getSupplierPower());
        releaseSlip.setPrisonPower(super.getPrisonPower());
        List<ReleaseSlip> list = releaseSlipService.list(releaseSlip);
        return new AppResponse(new PageInfo<>(list));
    }

    /**
     * 发货单详情查询
     * @param releaseSlipDetail；
     * @return 发货单详情列表
     */
    @DisableLoginCheck
    @RequestMapping("/listReleaseSlipDetail")
    public BaseResponse listReleaseSlipDetail(ReleaseSlipDetail releaseSlipDetail){
        List<ReleaseSlipDetail> list = releaseSlipDetailService.list(releaseSlipDetail);
        return new AppResponse(list);
    }
}
